using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Common;

namespace Microsoft.ConnectionEngine.Administration.Core.ServiceProvider.Business
{
    /// <summary>
    /// Exposes methods that allow to communicate with the Routing Services 
    /// </summary>
    public class PoolManager
    {        
        /// <summary>
        /// Searches for a list of pools by name or by serviceProviderType
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <returns></returns>
        public static Pools Search(HSDPrincipal currentPrincipal, PoolSearchRequest poolSearchRequest)
        {
            string searchRequestXml = AdapterHelper.ConstructObjectXml(poolSearchRequest, typeof(PoolSearchRequest));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, searchRequestXml);  
            
            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.PoolSearchRequest), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            
            return (Pools)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(Pools));
            
        }

        /// <summary>
        /// Select a pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolID"></param>
        /// <returns></returns>
        public static Pool SelectItem(HSDPrincipal currentPrincipal, int poolID)
        {            
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "PoolID", poolID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1066, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
           
            return (Pool)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(Pool));
        }


        /// <summary>
        /// Locks a Pool for the current user
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolID"></param>
        /// <returns>Pool - If successful, null otherwise</returns>
        public static Pool Lock(HSDPrincipal currentPrincipal, int poolID)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "PoolID", poolID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.PoolCheckOut), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            
            return (Pool)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(Pool));                    
        }

        /// <summary>
        /// Unlocks a Pool locked by the current user
        /// </summary>
        /// <param name="poolID"></param>
        /// <returns>Pool - If successful, null otherwise</returns>
        public static bool Unlock(HSDPrincipal currentPrincipal, int poolID)
        {
              if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "PoolID", poolID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.PoolCheckIn), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            
            return AdapterHelper.GetBooleanFromResponse(result);                  
        }

        /// <summary>
        /// Unlocks all Pools locked by the current user
        /// </summary>
        public static bool UnlockAll(HSDPrincipal currentPrincipal)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2] { RoleTypes.Administrator, RoleTypes.Provider }))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "PoolUnlockAll", "");

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.PoolCheckInAll), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            
            return AdapterHelper.GetBooleanFromResponse(result);
        }


        /// <summary>
        /// Add new pool to the Service Provider Register
        /// </summary>
        /// <param name="pool"></param>
        /// <returns>the unique id value of the newly created pool</returns>
        public static int Insert(HSDPrincipal currentPrincipal, Pool pool)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string poolXml = AdapterHelper.ConstructObjectXml(pool, typeof(Pool));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, poolXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.PoolInsert), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return ((Pool)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(Pool))).PoolID;
        }


        /// <summary>
        /// Updates a Pool
        /// </summary>
        /// <param name="poolID"></param>
        /// <returns>True - If successful, false otherwise</returns>
        public static bool Update(HSDPrincipal currentPrincipal, Pool pool)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string poolXml = AdapterHelper.ConstructObjectXml(pool, typeof(Pool));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, poolXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.PoolUpdate), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);            
        }

        /// <summary>
        /// Delete a pool
        /// </summary>
        /// <param name="poolID"></param>
        /// <returns>True - If successful, false otherwise</returns>
        public static bool Delete(HSDPrincipal currentPrincipal, int poolID)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "PoolID", poolID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.PoolDelete), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);
        }      
              
    }
}
